/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package demogroup5server.data;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author DungTV
 */
public class Connect {
    private Connection connection = null;
    private String url = "jdbc:sqlserver://localhost:1433;databaseName=DemoGroup5";
    private String driverClass = "com.microsoft.sqlserver.jdbc.SQLServerDriver";  
    private String uName = "group5";
    private String pass = "group5";
    

    
    public Connection getConnection() {
        return connection;
    }
    
    private static Connect instance;
    private Connect() {
    }
    public static Connect getInstance(){
        if (instance==null){
            instance = new Connect();
        }
        return instance;
    }
    public void connectDatabase() throws SQLException {
        try {
            Class.forName(driverClass);
            System.out.println("Driver Ok");
            try {
                connection = DriverManager.getConnection(url, uName, pass);
            } catch (SQLException ex) {
                Logger.getLogger(Connect.class.getName()).log(Level.SEVERE, null, ex);
            }
            System.out.println("Connected");
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(Connect.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public void disconnect() {
        if (connection != null) {
            try {
                connection.close();
                System.out.println("Disconnected");
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

}
